<template>
  <div class="health-management">
    <h2>健康管理</h2>
    <div class="toolbar">
      <el-button type="primary" @click="handleAddRecord">新增记录</el-button>
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="margin-left: 20px; width: 300px;"
        value-format="YYYY-MM-DD"
      />
      <el-select v-model="filterType" placeholder="筛选类型" style="margin-left: 20px; width: 150px;">
        <el-option label="全部" value=""></el-option>
        <el-option label="体检记录" value="体检"></el-option>
        <el-option label="诊断记录" value="诊断"></el-option>
        <el-option label="治疗记录" value="治疗"></el-option>
      </el-select>
    </div>
    
    <el-tabs v-model="activeTab" @tab-change="handleTabChange">
      <el-tab-pane label="患者列表" name="patients">
        <vxe-table
        :data="filteredPatients"
        border
        auto-resize
        resizable
      >
        <vxe-column field="id" title="ID"></vxe-column>
        <vxe-column field="name" title="姓名"></vxe-column>
        <vxe-column field="gender" title="性别"></vxe-column>
        <vxe-column field="age" title="年龄"></vxe-column>
        <vxe-column field="lastVisit" title="最近就诊"></vxe-column>
        <vxe-column field="condition" title="健康状况">
          <template #default="{ row }">
            <el-tag :type="getConditionType(row.condition)">{{ row.condition }}</el-tag>
          </template>
        </vxe-column>
        <vxe-column title="操作">
          <template #default="{ row }">
            <el-button size="small" @click="viewRecords(row)">查看记录</el-button>
            <el-button size="small" type="primary" @click="addRecord(row)">新增记录</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      </el-tab-pane>
      
      <el-tab-pane label="健康记录" name="records">
        <vxe-table
        :data="filteredRecords"
        border
        auto-resize
        resizable
      >
        <vxe-column field="id" title="记录ID"></vxe-column>
        <vxe-column field="patientName" title="患者姓名"></vxe-column>
        <vxe-column field="recordType" title="记录类型">
          <template #default="{ row }">
            <el-tag :type="getRecordTypeTag(row.recordType)">{{ row.recordType }}</el-tag>
          </template>
        </vxe-column>
        <vxe-column field="recordDate" title="记录日期"></vxe-column>
        <vxe-column field="doctor" title="医生"></vxe-column>
        <vxe-column field="diagnosis" title="诊断结果"></vxe-column>
        <vxe-column title="操作">
          <template #default="{ row }">
            <el-button size="small" @click="viewDetail(row)">查看详情</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      </el-tab-pane>
    </el-tabs>
    
    <div class="pagination">
      <vxe-pager
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50]"
        :total="totalItems"
        :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump']"
        @page-change="handlePageChange"
      />
    </div>
    
    <!-- 健康记录详情对话框 -->
    <el-dialog v-model="detailDialogVisible" :title="detailDialogTitle" width="600px">
      <el-form :model="currentRecord" label-width="100px">
        <el-form-item label="记录类型">
          <el-input v-model="currentRecord.recordType" readonly />
        </el-form-item>
        <el-form-item label="记录日期">
          <el-input v-model="currentRecord.recordDate" readonly />
        </el-form-item>
        <el-form-item label="医生">
          <el-input v-model="currentRecord.doctor" readonly />
        </el-form-item>
        <el-form-item label="诊断结果">
          <el-input v-model="currentRecord.diagnosis" type="textarea" :rows="3" readonly />
        </el-form-item>
        <el-form-item label="治疗建议">
          <el-input v-model="currentRecord.treatment" type="textarea" :rows="3" readonly />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="currentRecord.remarks" type="textarea" :rows="2" readonly />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 模拟患者数据
const mockPatients = [
  { id: 1, name: '张三', gender: '男', age: 45, lastVisit: '2023-10-15 14:30', condition: '稳定' },
  { id: 2, name: '李四', gender: '女', age: 38, lastVisit: '2023-10-20 09:15', condition: '康复中' },
  { id: 3, name: '王五', gender: '男', age: 52, lastVisit: '2023-09-28 11:45', condition: '需关注' },
  { id: 4, name: '赵六', gender: '女', age: 29, lastVisit: '2023-11-05 16:20', condition: '良好' },
  { id: 5, name: '钱七', gender: '男', age: 63, lastVisit: '2023-10-30 10:30', condition: '稳定' }
]

// 模拟健康记录数据
const mockRecords = [
  { id: 101, patientName: '张三', recordType: '体检', recordDate: '2023-10-15', doctor: '李医生', diagnosis: '血压偏高，建议低盐饮食', treatment: '每日测量血压，定期复查', remarks: '' },
  { id: 102, patientName: '李四', recordType: '诊断', recordDate: '2023-10-20', doctor: '王医生', diagnosis: '轻微颈椎病', treatment: '物理治疗，避免长时间低头', remarks: '症状较轻，注意日常姿势' },
  { id: 103, patientName: '王五', recordType: '治疗', recordDate: '2023-09-28', doctor: '张医生', diagnosis: '糖尿病', treatment: '控制饮食，按时服药，定期监测血糖', remarks: '需长期管理' },
  { id: 104, patientName: '赵六', recordType: '体检', recordDate: '2023-11-05', doctor: '刘医生', diagnosis: '各项指标正常', treatment: '继续保持健康生活方式', remarks: '年度体检结果良好' },
  { id: 105, patientName: '钱七', recordType: '诊断', recordDate: '2023-10-30', doctor: '陈医生', diagnosis: '轻度脂肪肝', treatment: '调整饮食结构，适量运动', remarks: '无需药物治疗' }
]

const patients = ref(mockPatients)
const records = ref(mockRecords)
const dateRange = ref('')
const filterType = ref('')
const activeTab = ref('patients')
const currentPage = ref(1)
const pageSize = ref(10)
const totalItems = ref(0)
const detailDialogVisible = ref(false)
const detailDialogTitle = ref('')
const currentRecord = ref({})

// 计算过滤后的患者数据
const filteredPatients = computed(() => {
  let result = patients.value
  
  // 根据日期范围和类型筛选（这里简化处理）
  if (filterType.value) {
    // 在实际应用中，这里应该根据筛选条件过滤数据
  }
  
  // 分页处理
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return result.slice(start, end)
})

// 计算过滤后的记录数据
const filteredRecords = computed(() => {
  let result = records.value
  
  // 根据日期范围和类型筛选（这里简化处理）
  if (filterType.value) {
    result = result.filter(record => record.recordType.includes(filterType.value))
  }
  
  // 分页处理
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return result.slice(start, end)
})

// 获取健康状况标签类型
const getConditionType = (condition) => {
  switch (condition) {
    case '良好':
      return 'success'
    case '稳定':
      return ''
    case '康复中':
      return 'warning'
    case '需关注':
      return 'danger'
    default:
      return 'info'
  }
}

// 获取记录类型标签类型
const getRecordTypeTag = (type) => {
  switch (type) {
    case '体检':
      return ''
    case '诊断':
      return 'success'
    case '治疗':
      return 'warning'
    default:
      return 'info'
  }
}

// 处理标签页切换
const handleTabChange = (tabName) => {
  activeTab.value = tabName
  currentPage.value = 1
}

// 处理分页变化
const handlePageChange = ({ currentPage, pageSize }) => {
  // 这里可以添加任何需要的逻辑
  console.log('分页变化:', currentPage, pageSize)
}

// 处理新增记录
const handleAddRecord = () => {
  ElMessage.info('新增健康记录功能待开发')
}

// 查看记录
const viewRecords = (patient) => {
  ElMessage.info(`查看患者 ${patient.name} 的健康记录`)
  activeTab.value = 'records'
}

// 为患者新增记录
const addRecord = (patient) => {
  ElMessage.info(`为患者 ${patient.name} 新增健康记录`)
}

// 查看详情
const viewDetail = (record) => {
  detailDialogTitle.value = '健康记录详情'
  currentRecord.value = { ...record }
  detailDialogVisible.value = true
}

// 组件挂载时获取数据
onMounted(() => {
  // 这里可以调用API获取真实的患者和健康记录数据
  console.log('获取健康数据...')
  totalItems.value = activeTab.value === 'patients' ? patients.value.length : records.value.length
})
</script>

<style scoped>
.health-management {
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}

.toolbar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.toolbar .el-input,
.toolbar .el-select,
.toolbar .el-date-picker {
  margin-right: 10px;
  margin-bottom: 10px;
}

.toolbar .el-button {
  margin-bottom: 10px;
}

.vxe-table {
  width: 100% !important;
  table-layout: fixed;
}

.vxe-table .vxe-cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .health-management {
    padding: 10px;
  }
  
  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  
  .toolbar .el-input,
  .toolbar .el-select,
  .el-date-editor {
    margin-right: 0;
  }
  
  .pagination {
    justify-content: center;
  }
  
  .el-dialog {
    width: 90% !important;
  }
}
</style>